package com.ruoyi.web.controller.tool;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.redis.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test/czz")
public class CzzTestController {

    private static final Logger log = LoggerFactory.getLogger(CzzTestController.class);

    //@Autowired
    //private Redisson redisson;

    @Autowired
    private RedisCache redisCache;



    private String lockKey ="product_101";

    @Anonymous
    @GetMapping("/test")
    public String test(){

        //RLock redissonLock = redisson.getLock(lockKey);
        //redissonLock.lock();

        try{
            int stock = redisCache.getCacheObject("stock");
            if (stock > 0) {
                int realStock = stock - 1;
                redisCache.setCacheObject("stock", realStock);
                log.error("剩余库存：" + realStock);
            } else {
                log.error("库存不足！");
            }
        }catch (Exception e){

        }finally {
            //redissonLock.unlock();
        }


        return "end";
    }
}
